package hevs.utils;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Context;

/**
 * This class contents the methods using by all application
 * @author Jonathan Moreira, Jun Yang, Natal Venetz, Ivan Samardziev
 *
 */
public class Utils {
	
	/**
	 * Method to get the CRUD confirmation (after having updated the DB)
	 * @param jArray
	 * @param actionName
	 * @param context
	 * @return pass
	 */
	public static boolean getCRUDconfirmation(JSONArray jArray, String actionName, Context context)
	{
		String message = "";
	
		
		if(jArray == null)
		{
			Error.toastNotif(context, "Wrong input information, Please check.", 2);
			return false;
		}
			
			int isWorking = -1;
			String objectName = "";
			
			try 
			{
				// to change to is working
				isWorking = jArray.getJSONObject(0).getInt("idSmartObject");
				
				if(isWorking > 0)
				{
					objectName = jArray.getJSONObject(0).getString("name");
					message += objectName+" has " + actionName +"ed correctly"; 
					Error.toastNotif(context, message, 3);
					return true;
				}
				
				else
				{
					message += actionName + "has failed, Please try again";
					Error.toastNotif(context, message, 4);
					return false;
				}
			
			} 
			catch (JSONException e) 
			{
				e.printStackTrace();
				return false;
			}
	} 
	/**
	 * Method to get the confirmation after the registration of the user
	 * @param jArray
	 * @param context
	 * @return pass
	 */
	public static boolean getRegisterConfirmation(JSONArray jArray, Context context)
	{
		
		String message = "";
	
		
		if(jArray == null)
		{
			Error.toastNotif(context, "Wrong input information, Please check.", 2);
			return false;
		}
			
			int isWorking = -1;
			String userName = "";
			
			try 
			{
				
				isWorking = jArray.getJSONObject(0).getInt("idUser");
				
				if(isWorking > 0)
				{
					userName = jArray.getJSONObject(0).getString("username");
					message += userName+" has registered correctly"; 
					Error.toastNotif(context, message, 3);
					return true;
				}
				
				else
				{
					message += "Registration has failed, Please try again";
					Error.toastNotif(context, message, 4);
					return false;
				}
			
			} 
			catch (JSONException e) 
			{
				e.printStackTrace();
				return false;
			}
	} 
}
